<?php
class AAppSmsKute extends CActiveRecord
{
    public function getAllApp($search, $currentPage, $numberRecordPerPage)
    {
        $startRecord = ($currentPage - 1) * $numberRecordPerPage;
        $condition = "";
        if(!empty($search["keyword"])){
            $keyword = str_replace(" ", "%", $search["keyword"]);
            
            if($search["keyword_field"]==1){
                $condition .= " AND title LIKE ('%" . $keyword . "%')";
            } else if($search["keyword_field"]==2){
                $condition .= " AND username LIKE ('%" . $keyword . "%')";
            }
        }
        
        $condition .= !empty($search["toDate"]) ? " AND create_date>=" . strtotime($search["toDate"]) : "";
        $condition .= !empty($search["fromDate"]) ? " AND create_date<=" . strtotime($search["fromDate"]) : "";
        $condition .= $search["status"]!="" ? " AND status=" . intval($search["status"]) : "";
        $condition .= $search["price"]!="" ? " AND price=" . intval($search["price"]) : "";
        
        $sql = "SELECT * FROM c_app_sms_kute WHERE 1 " . $condition . " ORDER BY create_date DESC LIMIT " . $startRecord . ", " . $numberRecordPerPage;
        $command = Yii::app()->db->createCommand($sql);
        $rows = $command->queryAll($sql);
        return $rows;
    }
    
    public function getTotalApp($search)
    {
        $condition = "";
        if(!empty($search["keyword"])){
            $keyword = str_replace(" ", "%", $search["keyword"]);
            
            if($search["keyword_field"]==1){
                $condition .= " AND title LIKE ('%" . $keyword . "%')";
            } else if($search["keyword_field"]==2){
                $condition .= " AND username LIKE ('%" . $keyword . "%')";
            }
        }
        
        $condition .= !empty($search["toDate"]) ? " AND create_date>=" . strtotime($search["toDate"]) : "";
        $condition .= !empty($search["fromDate"]) ? " AND create_date<=" . strtotime($search["fromDate"]) : "";
        $condition .= $search["status"]!="" ? " AND status=" . intval($search["status"]) : "";
        $condition .= $search["price"]!="" ? " AND price=" . intval($search["price"]) : "";
        
        $sql = "SELECT count(id) FROM c_app_sms_kute WHERE 1 " . $condition;
        $command = Yii::app()->db->createCommand($sql);
        $rows = $command->queryRow($sql);
        return $rows["count(id)"];   
    }
    
    public function DeleteApp($id)
    {
        $sql = "DELETE FROM c_app_sms_kute WHERE id=" . $id;
        $command = Yii::app()->db->createCommand($sql);
        $result = $command->execute();    
        return $result;
    }
    
    public function getAppInfo($appId)
    {
        $sql = "SELECT * FROM c_app_sms_kute WHERE id=" . $appId;
        $command = Yii::app()->db->createCommand($sql);
        $rows = $command->queryRow($sql);
        return $rows;  
    }
    
    public function getSms($list_sms_id)
    {
        $sql = "SELECT * FROM c_sms_kute WHERE id IN (" . $list_sms_id . ")";
        $command = Yii::app()->db->createCommand($sql);
        $rows = $command->queryAll($sql);
        return $rows;
    }
}  
?>
